package com.gxa.modules.service.lx.impl;

import com.gxa.common.constant.PageInfo;
import com.gxa.common.vo.R2;
import com.gxa.modules.mapper.lx.GoodsManageMapper;
import com.gxa.modules.mapper.lx.GoodsMapper;
import com.gxa.modules.service.lx.GoodsManageService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class GoodsManageServiceImpl implements GoodsManageService {
    @Autowired(required = false)
    private GoodsManageMapper goodsManageMapper;

    @Override
    public R2 queryShopGoods(Map map) {
        if (map.get("nowPage") == null && map.get("pageSize") == null) {
            map.put("nowPage", PageInfo.NOWPAGE);
            map.put("pageSize", PageInfo.PAGESIZE);
        }
        Integer nowPage = ((Integer) map.get("nowPage") - 1) * (Integer) map.get("pageSize");
        map.put("nowPage", nowPage);
        Integer sumPage = goodsManageMapper.sumPage(map);
        List<Set> Goodslist = goodsManageMapper.queryShopGoods(map);
        List list = new ArrayList();
        list.add(sumPage);
        list.add(Goodslist);
        R2 r2 = new R2();
        if (list != null) {
            r2.setCode(200);
            r2.setMsg("查询成功");
            r2.setData(list);
        } else {
            r2.setCode(500);
            r2.setMsg("系统开小差了");
        }
        return r2;
    }

    @Override
    public R2 editBasicInfo(Map map) {
        R2 r2 = new R2();
        if ((Integer) map.get("goodsStyle") == 1) {
            r2.setCode(500);
            r2.setMsg("选品商品基本信息不可修改");
            return r2;
        } else {
            //新建
            if((Integer)map.get("way") == 1){
                    Integer result2 = goodsManageMapper.intoBasicInfo(map);
                    int goodsId = goodsManageMapper.queryGoodsId(map);
                    if (result2 != 0) {
                        r2.setCode(200);
                        r2.setMsg("新建成功");
                        r2.setData(goodsId);
                        return r2;
                    }
            }
            //编辑
            if((Integer)map.get("way") == 2){
                    Integer result1 = goodsManageMapper.editBasicInfo(map);
                    if (result1 != 0) {
                        r2.setCode(200);
                        r2.setMsg("编辑成功");
                        return r2;
                    }

            }


        }
        return r2;
    }

    @Override
    public R2 editPriceInfo(Map map) {
        R2 r2 = new R2();
        if((Integer)map.get("way") == 1){
            Integer result = goodsManageMapper.intoPrice(map);
            Integer result2 = goodsManageMapper.intoPriceInfo(map);
            if (result != 0 && result2 != 0) {
                r2.setCode(200);
                r2.setMsg("新建成功");
                return r2;
            }
        }
        //编辑
        if((Integer)map.get("way") == 2){
            Integer result1 = goodsManageMapper.editPriceInfo(map);
            if (result1 != 0) {
                r2.setCode(200);
                r2.setMsg("编辑成功");
                return r2;
            }

        }

        return null;
    }
}
